package com.yan_jiu_sheng.LeetCodeHot100.AC;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * https://leetcode.cn/problems/group-anagrams/description/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-05-24 10:30
 */
public class Test02 {

    public static void main(String[] args) {
//        String[] strs={"eat","tea","tan","ate","nat","bat"};
        String[] strs = {"a"};
        System.out.println(groupAnagrams(strs));
    }

    public static List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList<>();
        HashMap<String, List<String>> hashMap = new HashMap<>();

        for (int i = 0; i < strs.length; i++) {
            char[] charArray = strs[i].toCharArray();
            Arrays.sort(charArray);
            String s = Arrays.toString(charArray);
            if (hashMap.isEmpty()) {
                List<String> temp = new ArrayList<>();
                temp.add(strs[i]);
                hashMap.put(s, temp);
                continue;
            }
            if (hashMap.containsKey(s)) {
                hashMap.get(s).add(strs[i]);
            } else {
                List<String> temp = new ArrayList<>();
                temp.add(strs[i]);
                hashMap.put(s, temp);
            }
        }
        for (String s : hashMap.keySet()) {
            res.add(hashMap.get(s));
        }
        return res;
    }

}
